package de.zefania.api.impl;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;



public class BookNamesHandler {
	private Logger logger = Logger.getLogger(BookNamesHandler.class);
	
	private String version;
	private String[] languages;
	private HashMap<String,Integer> bookNameIndex = new HashMap<String,Integer>();
	private HashMap<String,ArrayList<BookName>> bNames = new HashMap<String,ArrayList<BookName>>();

	public BookNamesHandler(final File file) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
		BasicConfigurator.configure();
		logger.setLevel(Level.DEBUG);
		
		// initialize Document and XPath
		Document doc = XmlTools.getDocumentFromFile(file);		
		XPath xpath = XPathFactory.newInstance().newXPath();
		
		// read Version
		version = doc.getFirstChild().getAttributes().getNamedItem("version").getNodeValue();
		
		// read LanguageList
		NodeList nodes = doc.getDocumentElement().getElementsByTagName("ID");
		languages = new String[nodes.getLength()];
		for (int i=0;i<languages.length;i++) {
			Node descr = nodes.item(i).getAttributes().getNamedItem("descr");
			languages[i] = descr.getNodeValue();
		}
		// read booklists
		for (int i=0;i<languages.length;i++) {
			XPathExpression expr = xpath.compile("//ID[@descr='"+languages[i]+"']/BOOK");
			NodeList bookNodes = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);			
			ArrayList<BookName> bookNameList = new ArrayList<BookName>();
			for (int j=0;j<bookNameList.size();j++) {
			    	int bnumber = Integer.valueOf(bookNodes.item(i).getAttributes().getNamedItem("bnumber").getNodeValue());
			    	String shortName = bookNodes.item(i).getAttributes().getNamedItem("bshort").getNodeValue();
			    	String bookName = bookNodes.item(i).getTextContent();
			    	bookNameList.add(new BookName(bnumber, shortName, bookName));
			    	bookNameIndex.put(bookName, bnumber);
			    	bookNameIndex.put(shortName, bnumber);			    	
			}			
			this.bNames.put(languages[i], bookNameList);
		}
	}
	
	public final List<String> getLanguagePacks() {
		
		return new ArrayList<String>();
	}
	
	public final String getVersion() {
		return this.version;
	}
	
	public final String[] getLanguages() {
		/*
		NodeList nodes = doc.getDocumentElement().getElementsByTagName("ID");
		String[] languages = new String[nodes.getLength()];
		for (int i=0;i<languages.length;i++) {
			Node descr = nodes.item(i).getAttributes().getNamedItem("descr");
			languages[i] = descr.getNodeValue();
		}*/
		return this.languages;
	}
	/*
	public BookName[] getBookNameSet(String language) throws XPathExpressionException {
		/*XPath xpath = XPathFactory.newInstance().newXPath();
	    XPathExpression expr1 = xpath.compile("//ID[@descr='"+language+"']/BOOK");
	    NodeList bookNodes = (NodeList)expr1.evaluate(doc, XPathConstants.NODESET);
	    BookName[] bookNames = new BookName[bookNodes.getLength()];
	    
	    for (int i=0;i<bookNames.length;i++) {
	    	int bnumber = Integer.valueOf(bookNodes.item(i).getAttributes().getNamedItem("bnumber").getNodeValue());
	    	String shortName = bookNodes.item(i).getAttributes().getNamedItem("bshort").getNodeValue();
	    	String bookName = bookNodes.item(i).getTextContent();
	    	bookNames[i] = new BookName(bnumber, shortName, bookName);
	    }
	    //this.bookNames = bookNames;
	
	    //return bookNames;
	}*/
	
	public final int getBookNumber(final String bname) {
		for (String bookName : bookNameIndex.keySet()) {
			if (
					bname.startsWith(bookName) ||
					bookName.startsWith(bname) 
					) {
				return bookNameIndex.get(bookName);
			}
		}
		
		
		/*for ( name : bookNameIndex) {
			if (					
				bname.startsWith(name.getBookName()) || 
				bname.startsWith(name.getShortName()) ||
				name.getBookName().startsWith(bname) 
				
					) return name.getNumber();			
		}**/
		return 0;
		
	}
	
}
